package it.stasbranger.myproject.service;

import it.stasbranger.myproject.dao.RequestsDAO;
import it.stasbranger.myproject.dao.SuppliersDAO;
import it.stasbranger.myproject.dao.SuppliersHasRequestsDAO;

import it.stasbranger.myproject.domain.Requests;
import it.stasbranger.myproject.domain.Suppliers;
import it.stasbranger.myproject.domain.SuppliersHasRequests;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for SuppliersHasRequests entities
 * 
 */

@Service("SuppliersHasRequestsService")
@Transactional
public class SuppliersHasRequestsServiceImpl implements
		SuppliersHasRequestsService {

	/**
	 * DAO injected by Spring that manages Requests entities
	 * 
	 */
	@Autowired
	private RequestsDAO requestsDAO;

	/**
	 * DAO injected by Spring that manages Suppliers entities
	 * 
	 */
	@Autowired
	private SuppliersDAO suppliersDAO;

	/**
	 * DAO injected by Spring that manages SuppliersHasRequests entities
	 * 
	 */
	@Autowired
	private SuppliersHasRequestsDAO suppliersHasRequestsDAO;

	/**
	 * Instantiates a new SuppliersHasRequestsServiceImpl.
	 *
	 */
	public SuppliersHasRequestsServiceImpl() {
	}

	/**
	 * Delete an existing SuppliersHasRequests entity
	 * 
	 */
	@Transactional
	public void deleteSuppliersHasRequests(SuppliersHasRequests suppliershasrequests) {
		suppliersHasRequestsDAO.remove(suppliershasrequests);
		suppliersHasRequestsDAO.flush();
	}

	/**
	 * Save an existing Suppliers entity
	 * 
	 */
	@Transactional
	public SuppliersHasRequests saveSuppliersHasRequestsSuppliers(Integer suppliersIdsuppliers, Integer requestsIdrequests, Suppliers related_suppliers) {
		SuppliersHasRequests suppliershasrequests = suppliersHasRequestsDAO.findSuppliersHasRequestsByPrimaryKey(suppliersIdsuppliers, requestsIdrequests, -1, -1);
		Suppliers existingsuppliers = suppliersDAO.findSuppliersByPrimaryKey(related_suppliers.getIdsuppliers());

		// copy into the existing record to preserve existing relationships
		if (existingsuppliers != null) {
			existingsuppliers.setIdsuppliers(related_suppliers.getIdsuppliers());
			existingsuppliers.setName(related_suppliers.getName());
			existingsuppliers.setAddress(related_suppliers.getAddress());
			related_suppliers = existingsuppliers;
		}

		suppliershasrequests.setSuppliers(related_suppliers);
		related_suppliers.getSuppliersHasRequestses().add(suppliershasrequests);
		suppliershasrequests = suppliersHasRequestsDAO.store(suppliershasrequests);
		suppliersHasRequestsDAO.flush();

		related_suppliers = suppliersDAO.store(related_suppliers);
		suppliersDAO.flush();

		return suppliershasrequests;
	}

	/**
	 */
	@Transactional
	public SuppliersHasRequests findSuppliersHasRequestsByPrimaryKey(Integer suppliersIdsuppliers, Integer requestsIdrequests) {
		return suppliersHasRequestsDAO.findSuppliersHasRequestsByPrimaryKey(suppliersIdsuppliers, requestsIdrequests);
	}

	/**
	 * Return all SuppliersHasRequests entity
	 * 
	 */
	@Transactional
	public List<SuppliersHasRequests> findAllSuppliersHasRequestss(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<SuppliersHasRequests>(suppliersHasRequestsDAO.findAllSuppliersHasRequestss(startResult, maxRows));
	}

	/**
	 * Delete an existing Suppliers entity
	 * 
	 */
	@Transactional
	public SuppliersHasRequests deleteSuppliersHasRequestsSuppliers(Integer suppliershasrequests_suppliersIdsuppliers, Integer suppliershasrequests_requestsIdrequests, Integer related_suppliers_idsuppliers) {
		SuppliersHasRequests suppliershasrequests = suppliersHasRequestsDAO.findSuppliersHasRequestsByPrimaryKey(suppliershasrequests_suppliersIdsuppliers, suppliershasrequests_requestsIdrequests, -1, -1);
		Suppliers related_suppliers = suppliersDAO.findSuppliersByPrimaryKey(related_suppliers_idsuppliers, -1, -1);

		suppliershasrequests.setSuppliers(null);
		related_suppliers.getSuppliersHasRequestses().remove(suppliershasrequests);
		suppliershasrequests = suppliersHasRequestsDAO.store(suppliershasrequests);
		suppliersHasRequestsDAO.flush();

		related_suppliers = suppliersDAO.store(related_suppliers);
		suppliersDAO.flush();

		suppliersDAO.remove(related_suppliers);
		suppliersDAO.flush();

		return suppliershasrequests;
	}

	/**
	 * Return a count of all SuppliersHasRequests entity
	 * 
	 */
	@Transactional
	public Integer countSuppliersHasRequestss() {
		return ((Long) suppliersHasRequestsDAO.createQuerySingleResult("select count(*) from SuppliersHasRequests o").getSingleResult()).intValue();
	}

	/**
	 * Save an existing SuppliersHasRequests entity
	 * 
	 */
	@Transactional
	public void saveSuppliersHasRequests(SuppliersHasRequests suppliershasrequests) {
		SuppliersHasRequests existingSuppliersHasRequests = suppliersHasRequestsDAO.findSuppliersHasRequestsByPrimaryKey(suppliershasrequests.getSuppliersIdsuppliers(), suppliershasrequests.getRequestsIdrequests());

		if (existingSuppliersHasRequests != null) {
			if (existingSuppliersHasRequests != suppliershasrequests) {
				existingSuppliersHasRequests.setSuppliersIdsuppliers(suppliershasrequests.getSuppliersIdsuppliers());
				existingSuppliersHasRequests.setRequestsIdrequests(suppliershasrequests.getRequestsIdrequests());
				existingSuppliersHasRequests.setSenddate(suppliershasrequests.getSenddate());
				existingSuppliersHasRequests.setPrice(suppliershasrequests.getPrice());
			}
			suppliershasrequests = suppliersHasRequestsDAO.store(existingSuppliersHasRequests);
		} else {
			suppliershasrequests = suppliersHasRequestsDAO.store(suppliershasrequests);
		}
		suppliersHasRequestsDAO.flush();
	}

	/**
	 * Load an existing SuppliersHasRequests entity
	 * 
	 */
	@Transactional
	public Set<SuppliersHasRequests> loadSuppliersHasRequestss() {
		return suppliersHasRequestsDAO.findAllSuppliersHasRequestss();
	}

	/**
	 * Delete an existing Requests entity
	 * 
	 */
	@Transactional
	public SuppliersHasRequests deleteSuppliersHasRequestsRequests(Integer suppliershasrequests_suppliersIdsuppliers, Integer suppliershasrequests_requestsIdrequests, Integer related_requests_idrequests) {
		SuppliersHasRequests suppliershasrequests = suppliersHasRequestsDAO.findSuppliersHasRequestsByPrimaryKey(suppliershasrequests_suppliersIdsuppliers, suppliershasrequests_requestsIdrequests, -1, -1);
		Requests related_requests = requestsDAO.findRequestsByPrimaryKey(related_requests_idrequests, -1, -1);

		suppliershasrequests.setRequests(null);
		related_requests.getSuppliersHasRequestses().remove(suppliershasrequests);
		suppliershasrequests = suppliersHasRequestsDAO.store(suppliershasrequests);
		suppliersHasRequestsDAO.flush();

		related_requests = requestsDAO.store(related_requests);
		requestsDAO.flush();

		requestsDAO.remove(related_requests);
		requestsDAO.flush();

		return suppliershasrequests;
	}

	/**
	 * Save an existing Requests entity
	 * 
	 */
	@Transactional
	public SuppliersHasRequests saveSuppliersHasRequestsRequests(Integer suppliersIdsuppliers, Integer requestsIdrequests, Requests related_requests) {
		SuppliersHasRequests suppliershasrequests = suppliersHasRequestsDAO.findSuppliersHasRequestsByPrimaryKey(suppliersIdsuppliers, requestsIdrequests, -1, -1);
		Requests existingrequests = requestsDAO.findRequestsByPrimaryKey(related_requests.getIdrequests());

		// copy into the existing record to preserve existing relationships
		if (existingrequests != null) {
			existingrequests.setIdrequests(related_requests.getIdrequests());
			existingrequests.setDate(related_requests.getDate());
			existingrequests.setName(related_requests.getName());
			existingrequests.setEmail(related_requests.getEmail());
			related_requests = existingrequests;
		}

		suppliershasrequests.setRequests(related_requests);
		related_requests.getSuppliersHasRequestses().add(suppliershasrequests);
		suppliershasrequests = suppliersHasRequestsDAO.store(suppliershasrequests);
		suppliersHasRequestsDAO.flush();

		related_requests = requestsDAO.store(related_requests);
		requestsDAO.flush();

		return suppliershasrequests;
	}
}
